
package privatemessanger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * This class will manage all of the creatting a server popup window, 2 sides- logo and user interface.
 * @author  Erez Fatal, Oren Slor, Elad Yanai, Sigal Agami, Chen Ben Shabat,
 *          Inbar Pardo, Islamm Heralla, Ziv Hadary, Oren Shalev, Ortal David.
 */
public class HostPanel extends JPanel implements ActionListener
{
    private static String HOST= "Creat";
    private static String CANCEL = "Cancel";
    private LogoPanel _logoPanel;
    private UserPanel _userPanel;
    private JFrame controllingFrame; //needed for dialogs
    private MainFrame _mainFrame;

    /**
     * Setting up all the needed parameters.
     * @param f
     * @param mainFrame
     */
    public HostPanel( JFrame f, MainFrame mainFrame)
    {
         _mainFrame = mainFrame;

         controllingFrame = f;
         _logoPanel = new LogoPanel();
         _userPanel = new UserPanel(this, _mainFrame);

         f.getContentPane().add(_logoPanel, "West");
         f.getContentPane().add(_userPanel,"Center");
         _logoPanel.setBackground(Color.white);
         _userPanel.setBackground(Color.white);
    }

    public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); }

    /**
     * This class will create and lay the user side panels and components
     */
    public class UserPanel extends JPanel implements ActionListener, KeyListener
    {
        private JTextField _MyIPfield, _PortField, _ServerNameField;
        private HostPanel _p;
        private MainFrame _mainFrame;
        private JRadioButton _isPublicButton;
        private JRadioButton _isPrivateButton;
        private boolean _isOpen=true;

        public UserPanel(HostPanel p, MainFrame mainFrame)
        {
            _mainFrame= mainFrame;
            _p = p;
            PaintComponent();
        }

        /**
         *  If the user pressed "ENTER" and not the buttons, do the same as if the user pressed on confirm.
         * @param e
         */
        public void keyTyped(KeyEvent e)
        {
           if(e.getKeyCode()== KeyEvent.VK_ENTER)
           {
                String cmd = "Creat";
                Object source = e.getSource();

                if (source==_isPrivateButton)
                {
                    _isOpen=false;
                    return;
                }

                if (source==_isPublicButton)
                {
                    _isOpen=true;
                    return;
                }

                if (HOST.equals(cmd))
                {
                    // if the user pressed on Login

                    if(_ServerNameField.getText().length()==0)
                    {
                        JOptionPane.showMessageDialog(controllingFrame, "Invalid Server Name. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    int isServerNameSpace=_ServerNameField.getText().indexOf(" ");
                    if(isServerNameSpace != -1)
                    {
                        JOptionPane.showMessageDialog(controllingFrame, "No Space Allowed in Server Name. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    controllingFrame.dispose();
                    Server server=new Server(_mainFrame.getUser(),_ServerNameField.getText(),_isOpen);
                    _mainFrame.getUser().addServer(server);
                    server.start();
                    MainFrame._isServer=true;
                    _mainFrame.setVisible(true);
                    _mainFrame.hostSucceed();
                }
                else
                {
                    //The user has pressed on Cancel.
                    controllingFrame.dispose();
                    _mainFrame.setVisible(true);
                }
            }
        }

        /**
         * Laying all the components in the right order.
         */
        private void PaintComponent()
        {
            String _IP="";

            try {
                _IP = java.net.InetAddress.getLocalHost().getHostAddress();
            } catch(Exception error){error.printStackTrace();}

              //Lay out everything.

            JLabel _linedown = new JLabel("this is a Line                                          Down:   ");
            _linedown.setForeground(Color.white);
            _p.add(_linedown);
            _linedown.setVisible(true);

            JLabel _MyIP = new JLabel("Your IP is:  ");
            _p.add(_MyIP);
            _MyIP.setVisible(true);
            _p.setBackground(Color.white);

            _MyIPfield = new JTextField(9);
            _MyIPfield.setText(_IP);
            _MyIPfield.setEditable(false);
            _MyIPfield.addActionListener(this);
            _p.add(_MyIPfield);
            _MyIPfield.setVisible(true);

            JLabel _linedown3 = new JLabel("this is a Line                                          Down:    ");
            _linedown3.setForeground(Color.white);
            _p.add(_linedown3);
            _linedown3.setVisible(true);

            JLabel _ServerName = new JLabel("Enter Server Name: ");
            _p.add(_ServerName);
            _ServerName.setVisible(true);

            _ServerNameField= new JTextField(9);
            _ServerNameField.addActionListener(this);
            _ServerNameField.addKeyListener(this);
            _p.add(_ServerNameField);
            _ServerNameField.requestFocusInWindow();

            _ServerNameField.setVisible(true);

            _isPublicButton = new JRadioButton("Public Server");
            _isPublicButton.setMnemonic(KeyEvent.VK_B);
            _isPublicButton.setActionCommand("Public Server");
            _isPublicButton.setBackground(Color.white);
            _isPublicButton.setSelected(true);
            _isPublicButton.addActionListener(this);
            _p.add(_isPublicButton);

            _isPrivateButton = new JRadioButton("Private Server");
            _isPrivateButton.setMnemonic(KeyEvent.VK_B);
            _isPrivateButton.setActionCommand("Private Server");
            _isPrivateButton.setBackground(Color.white);
            _isPrivateButton.addActionListener(this);
            _p.add(_isPrivateButton);

            ButtonGroup group = new ButtonGroup();
            group.add(_isPublicButton);
            group.add(_isPrivateButton);

            JComponent buttonPane = createButtonPanel();

            JLabel _linedown2 = new JLabel("this is a Line                                          Down:    ");
            _linedown2.setForeground(Color.white);
            _p.add(_linedown2);

            _p.add(buttonPane);
            _p.repaint();
        }

        /**
         * Setting up the button panel.
         * @return
         */
        protected JComponent createButtonPanel()
        {
            JPanel p = new JPanel(new GridLayout(0,2));
            JButton HostButton = new JButton("Host");
            JButton cancelButton = new JButton("Cancel");

            HostButton.setActionCommand(HOST);
            cancelButton.setActionCommand(CANCEL);
            cancelButton.addActionListener(this);
              HostButton.addActionListener(this);
            _mainFrame.getRootPane().setDefaultButton(HostButton);

            p.add(HostButton);
            p.add(cancelButton);

            return p;
        }

        /**
         * If the user pressed on the buttons then do the logic (check the input) and do your action.
         * @param e
         */
        public void actionPerformed(ActionEvent e)
        {
            String cmd = e.getActionCommand();
            Object source = e.getSource();

            if (source==_isPrivateButton)
            {
                _isOpen=false;
                return;
            }

            if (source==_isPublicButton)
            {
                _isOpen=true;
                return;
            }

            if (HOST.equals(cmd))
            {
                // if the user pressed on Login

                if(_ServerNameField.getText().length()==0)
                {
                    JOptionPane.showMessageDialog(controllingFrame, "Invalid Server Name. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                int isServerNameSpace=_ServerNameField.getText().indexOf(" ");
                if(isServerNameSpace != -1)
                {
                    JOptionPane.showMessageDialog(controllingFrame, "No Space Allowed in Server Name. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                controllingFrame.dispose();
                Server server=new Server(_mainFrame.getUser(),_ServerNameField.getText(),_isOpen);
                _mainFrame.getUser().addServer(server);
                server.start();
                MainFrame._isServer=true;
                _mainFrame.setVisible(true);
                _mainFrame.hostSucceed();
            }
            else
            {
                //The user has pressed on Cancel.
                controllingFrame.dispose();
                _mainFrame.setVisible(true);
            }
        }

        public void keyPressed(KeyEvent e) { keyTyped(e); }

        public void keyReleased(KeyEvent e) { }
    }

    /**
     *  Setting up the logo side of the frame.
     */
    public class LogoPanel extends JPanel
    {
        private JLabel _logo;
        private JFrame _f;

        public LogoPanel()
        {
            setBackground(Color.white);
            ImageIcon _icon = createImageIcon("logo.jpg", "Register to Messenger now!");
            _logo = new JLabel(_icon);
            add(_logo);
            _logo.setVisible(true);
            repaint();
        }

        /**
         * Create the logo image and lay it down.
         * @param path
         * @param description
         * @return
         */
        protected ImageIcon createImageIcon(String path, String description)
        {
            java.net.URL imgURL = LogoPanel.class.getResource(path);
            if (imgURL != null)
            {
                return new ImageIcon(imgURL, description);
            }
            else
            {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    }
}
